package com.example.huwang.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.huwang.R;
import com.example.huwang.model.Icon;

import java.util.List;

public class IconAdapter extends BaseAdapter {

    private List<Icon> iconList;
    private LayoutInflater layoutInflater;

    public IconAdapter(Context context, List<Icon> iconList) {
        this.iconList = iconList;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return iconList.size();
    }

    @Override
    public Object getItem(int position) {
        return iconList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.item_grid_icon, null);
            holder = new ViewHolder();
            holder.txt_icon = (TextView) convertView.findViewById(R.id.txt_icon);
            holder.img_icon = (ImageView) convertView.findViewById(R.id.img_icon);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Icon icon = iconList.get(position);
        if (icon != null) {
            holder.txt_icon.setText(icon.getName());
            holder.img_icon.setImageResource(icon.getId());
        }
        return convertView;
    }

    class ViewHolder {
        TextView txt_icon;
        ImageView img_icon;
    }

}
